
package com.tenglong.teacher.ui.utils;

import android.content.Context;
import android.util.SparseArray;

import com.cuotibao.zhuoxiao.basics.utils.JsonUtils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Stack;

public class CommonDao {

    // 行业信息
    public static class DicItem {
        public int id;
        public String name;
        public int mParentId;
    }

    // 地区信息
    public static class AreaInfo {
        public int mAreaId;
        public String mAreaName;
        public int mLevel;
        public int mParentId;
    }

    // 职位信息
    public static class PostionInfo {
        public int mPosId;
        public String mPosName;
        public int mLevel;
        public int mParentId;
    }

    // 字典信息
    public static class DictInfo {
        public int id;
        public String code;
        public String name;
        public int ordernum;
        public String descinfor;
        public int pid;
        public String subsystem;
    }

    private static CommonDao instance;

    private Map<String, List<DicItem>> hy;
    private Map<String, List<DictInfo>> dict;
    private SparseArray<AreaInfo> mAreaInfo = new SparseArray<AreaInfo>();
    private SparseArray<PostionInfo> mPosInfo = new SparseArray<PostionInfo>();
    private SparseArray<DicItem> mHyInfo = new SparseArray<DicItem>();
    private SparseArray<DictInfo> mWantjobInfo = new SparseArray<DictInfo>();
    private SparseArray<DictInfo> mClassInfo = new SparseArray<DictInfo>();
    private SparseArray<DictInfo> mEduInfo = new SparseArray<DictInfo>();

    public synchronized static CommonDao getInstance(Context context) {
        if (instance == null) {
            instance = new CommonDao();
            instance.init(context);
        }
        return instance;
    }

    private CommonDao() {

    }

    @SuppressWarnings("unchecked")
    private void init(Context context) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            InputStream in = context.getAssets().open("document.json");
            Map<String, Object> allData = mapper.readValue(in, Map.class);
            hy = (Map<String, List<DicItem>>) allData.get("hy");
            dict = (Map<String, List<DictInfo>>) allData.get("dictionary");
            List<Map<String, Object>> zw = (List<Map<String, Object>>) allData.get("zw");
            initZWInfo(zw);
            List<Map<String, Object>> adcd = (List<Map<String, Object>>) allData.get("adcd");

            initAreaInfo(adcd);
            initDicInfo(dict);
            initHYInfo(hy);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initZWInfo(List<Map<String, Object>> zw) {
        if (zw != null) {
            for (Map<String, Object> item : zw) {
                PostionInfo info = new PostionInfo();
                info.mPosId = Integer.parseInt((String) item.get("id"));
                info.mPosName = (String) item.get("name");
                info.mLevel = (Integer) item.get("lev");
                info.mParentId = Integer.parseInt((String) item.get("pid"));
                mPosInfo.put(info.mPosId, info);
            }
        }
    }

    private void initDicInfo(Map<String, List<DictInfo>> hy2) {
        if (hy2 != null) {

            for (String key : hy2.keySet()) {
                if (key.equals("jobHope")) {
                    List<DictInfo> l = hy2.get(key);

                    for (int i = 0; i < l.size(); i++) {
                        String df = JsonUtils.toJson(l.get(i), false);
                        DictInfo d = JsonUtils.fromJson(df, DictInfo.class);
                        mWantjobInfo.put(d.id, d);

                    }

                } else if (key.equals("class")) {
                    List<DictInfo> l = hy2.get(key);

                    for (int i = 0; i < l.size(); i++) {
                        String df = JsonUtils.toJson(l.get(i), false);
                        DictInfo d = JsonUtils.fromJson(df, DictInfo.class);
                        mClassInfo.put(d.id, d);

                    }

                }else if(key.equals("education")){
                	List<DictInfo> l = hy2.get(key);
                	 for (int i = 0; i < l.size(); i++) {
                	  String df = JsonUtils.toJson(l.get(i), false);
                      DictInfo d = JsonUtils.fromJson(df, DictInfo.class);
                      mEduInfo.put(d.id, d);
                	 }
                	
                }
            }

        }
    }

    private void initHYInfo(Map<String, List<DicItem>> hy2) {
        if (hy2 != null) {
            int topid = 1;
            for (String key : hy2.keySet()) {
                List<DicItem> l = hy2.get(key);
                if (l.size() > 0) {
                    DicItem top = new DicItem();
                    top.mParentId = 0;
                    top.id = topid;
                    top.name = key;
                    mHyInfo.put(top.id, top);

                    for (int i = 0; i < l.size(); i++) {
                        String df = JsonUtils.toJson(l.get(i), false);
                        DicItem d = JsonUtils.fromJson(df, DicItem.class);
                        d.mParentId = topid;
                        mHyInfo.put(d.id, d);

                    }
                    topid++;
                }
            }
        }
    }

    private void initAreaInfo(List<Map<String, Object>> adcd) {
        if (adcd != null) {
            for (Map<String, Object> item : adcd) {
                AreaInfo info = new AreaInfo();
                info.mAreaId = Integer.parseInt((String) item.get("adcd"));
                info.mAreaName = (String) item.get("adnm");
                info.mLevel = Integer.parseInt((String) item.get("adlevel"));
                info.mParentId = Integer.parseInt((String) item.get("parentid"));
                mAreaInfo.put(info.mAreaId, info);
            }
        }
    }

    public String getHyName(int hyId) {
        DicItem info = mHyInfo.get(hyId);
        return info != null ? info.name : "";
    }

    public String getPositionName(int poId) {
        PostionInfo info = mPosInfo.get(poId);
        return info != null ? info.mPosName : "";
    }

    public SparseArray<String> getMartialInfo() {
        SparseArray<String> result = new SparseArray<String>();

        for (int i = 0; i < mClassInfo.size(); i++) {
            DictInfo info = mClassInfo.valueAt(i);
            {
                result.put(info.id, info.name);
            }
        }
        return result;
    }

    public SparseArray<String> getEduInfo() {
        SparseArray<String> result = new SparseArray<String>();

        for (int i = 0; i < mEduInfo.size(); i++) {
            DictInfo info = mEduInfo.valueAt(i);
            {
                result.put(info.id, info.name);
            }
        }
        return result;
    }
    public SparseArray<String> getClassInfo() {
        SparseArray<String> result = new SparseArray<String>();

        for (int i = 0; i < mClassInfo.size(); i++) {
            DictInfo info = mClassInfo.valueAt(i);
            {
                result.put(info.id, info.name);
            }
        }
        return result;
    }
    public SparseArray<String> getHopeJobInfo() {
        SparseArray<String> result = new SparseArray<String>();

        for (int i = 0; i < mWantjobInfo.size(); i++) {
            DictInfo info = mWantjobInfo.valueAt(i);
            {
                result.put(info.id, info.name);
            }
        }
        return result;

    }

    public String getHopeJob(int id) {
        DictInfo info = mWantjobInfo.get(id);
        return info != null ? info.name : "";

    }

    public String getClassName(int code) {
        DictInfo info = mClassInfo.get(code);
        return info != null ? info.name : "";
    }
    
    public String getEduName(int code) {
        DictInfo info = mEduInfo.get(code);
        return info != null ? info.name : "";
    }

    public String getAreaName(int areaId) {
        AreaInfo info = mAreaInfo.get(areaId);
        return info != null ? info.mAreaName : "";
    }

    public String getAreaFullName(int areaId) {
        AreaInfo info = mAreaInfo.get(areaId);
        StringBuffer buffer = new StringBuffer();
        while (info != null) {
            buffer.insert(0, info.mAreaName).insert(0, "/");
            info = mAreaInfo.get(info.mParentId);
        }
        if (buffer.length() > 0) {
            buffer.deleteCharAt(0);
        }
        return buffer.toString();
    }
    public Stack<Integer> getAreaFullId(int areaId){
        Stack<Integer> result = new Stack<Integer>();
        AreaInfo info = mAreaInfo.get(areaId);

        while (info != null) {
            result.push(info.mAreaId);
            info = mAreaInfo.get(info.mParentId);
        }
        return result;

    }

    public SparseArray<String> getAreaList(int areaId) {
        SparseArray<String> result = new SparseArray<String>();

        for (int i = 0; i < mAreaInfo.size(); i++) {
            AreaInfo info = mAreaInfo.valueAt(i);
            if (info.mParentId == areaId) {
                result.put(info.mAreaId, info.mAreaName);
            }
        }
        return result;
    }

    public SparseArray<String> getZwList(int zwId) {
        SparseArray<String> result = new SparseArray<String>();

        for (int i = 0; i < mPosInfo.size(); i++) {
            PostionInfo info = mPosInfo.valueAt(i);
            if (info.mParentId == zwId) {
                result.put(info.mPosId, info.mPosName);
            }
        }
        return result;
    }

    public SparseArray<String> getHyList(int HyId) {
        SparseArray<String> result = new SparseArray<String>();

        for (int i = 0; i < mHyInfo.size(); i++) {
            DicItem info = mHyInfo.valueAt(i);
            if (info.mParentId == HyId) {
                result.put(info.id, info.name);
            }

        }
        return result;
    }
    // 公司规模

    public static class NameValue {
        public int id;
        public String value;

        NameValue(int id, String value) {
            this.id = id;
            this.value = value;
        }
    }

    // 公司规模
    public static final NameValue[] sCompanyScale = new NameValue[] {
            new NameValue(0, "不限"),
            new NameValue(10000032, "1-49人"),
            new NameValue(10000033, "50-99人"),
            new NameValue(10000034, "100-499人"),
            new NameValue(10000035, "500-999人"),
            new NameValue(10000036, "1000-2000人"),
            new NameValue(10000037, "2000-5000人"),
            new NameValue(10000038, "5000-10000人"),
            new NameValue(10000039, "10000人以上")
    };

    // 公司性质
    public static final NameValue[] sCompanyKind = new NameValue[] {
            new NameValue(0, "不限"),
            new NameValue(10000023, "外商独资/外企办事处"),
            new NameValue(10000024, "中外合营(合资/合作)"),
            new NameValue(10000025, "私营/民营企业"),
            new NameValue(10000026, "私营/民营企业"),
            new NameValue(10000027, "国内上市公司"),
            new NameValue(10000028, "政府机关／非盈利机构"),
            new NameValue(10000029, "事业单位"),
            new NameValue(10000030, "其他")
    };

    // 学历
    public static final NameValue[] sEducation = new NameValue[] {
            new NameValue(0, "不限"),
            new NameValue(10, "博士"),
            new NameValue(20, "硕士"),
            new NameValue(30, "本科"),
            new NameValue(40, "大专"),
            new NameValue(50, "中技"),
            new NameValue(60, "高中"),
    };

    public static String getCompanyType(int code) {
        for (NameValue nv : sCompanyKind) {
            if (nv.id == code) {
                return nv.value;
            }
        }
        return "";
    }

    public static String getCompanyScal(int code) {
        for (NameValue nv : sCompanyScale) {
            if (nv.id == code) {
                return nv.value;
            }
        }
        return "";
    }

    public static String getEducation(int code) {
        for (NameValue nv : sEducation) {
            if (nv.id == code) {
                return nv.value;
            }
        }
        return "";
    }
}
